package com.example.nfc.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.nfc.R;
import com.example.nfc.server.response.Rows;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Create by 游德禄 on 2019-9-21
 * 已采集列表 开机密码
 */
public class XjdkAdapter extends RecyclerView.Adapter<XjdkAdapter.RecyclerViewHolder>{
    private LayoutInflater inflater;
    private List<Rows> data = new ArrayList<>();
    private ClickCallBack callBack;


    public XjdkAdapter(Context context, ClickCallBack clickCallBack) {
        this.inflater= LayoutInflater.from(context);
        this.callBack = clickCallBack;
    }


    /**
     *
     * @param data
     */
    public void update(List<Rows> data){
        this.data.clear();
        this.data = new ArrayList<>(data);
        this.notifyDataSetChanged();
    }




    public int getSize(){
        return this.data.size();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public Rows getItem(int i) {
        if(data.size()>0)
            return data.get(i);
        else
            return null;
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        final Rows data = getItem(position);
        holder.daka.setTag(position);
        if(data!=null) {
            holder.text1.setText(data.getINFO_MC());
            String text = "";
            if(!TextUtils.isEmpty(data.getINFO_FRID())){
                text+="钢构件RFID:"+data.getINFO_FRID()+"\n";
            }
            if(!TextUtils.isEmpty(data.getINFO_GPS())){
                text+="GPS定位:"+data.getINFO_GPS()+"\n";
            }
            if(!TextUtils.isEmpty(data.getINFO_ZT())){
                text+="目前状态:"+getZT(data.getINFO_ZT())+"\n";
            }
            holder.text2.setText(text);
        }
    }
    public String getZT(String zt){
        switch (zt){
            case "xz":
                return "闲置";
            case "sy":
                return "使用";
            case "sh":
                return "损坏";
            case "djc":
                return "待检测";
        }
        return "";
    }

    @Override
    @NonNull
    public XjdkAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.xjdkypd_item, parent, false);
        return new XjdkAdapter.RecyclerViewHolder(view);
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.text1)
        TextView text1;
        @Bind(R.id.text2)
        TextView text2;
        @Bind(R.id.daka)
        Button daka;
        RecyclerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this,view);
            daka.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.open(position);
                    }
                }
            });
        }
    }
    public interface ClickCallBack {
         void open(int position);
    }
}
